package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.address.AddressMessage;
import com.masterclient.app.modules.order.NewActivityDialog;
import com.eoner.baselibrary.bean.order.ConfirmOrderCustomerAccount;
import com.eoner.baselibrary.bean.order.ConfirmOrderData;
import com.eoner.baselibrary.bean.order.ConfirmOrderInstructions;
import com.eoner.baselibrary.bean.order.ConfirmOrderPrice;
import com.eoner.baselibrary.bean.pay.PaymentModule;

import java.util.List;

public class SubmitOrderPayAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    ConfirmOrderData data;

    OnPayClick onPayClick;

    String saleType;



    public interface OnPayClick{
        void OnGoldChanged(boolean isGoldSelect);
        void OnBalanceChanged(boolean isBalanceSelect);
        void OnGZBChanged(boolean isGZBSelect);
        void OnPayWay(String payWay);
    }

    public OnPayClick getOnPayClick() {
        return onPayClick;
    }

    public void setOnPayClick(OnPayClick onPayClick) {
        this.onPayClick = onPayClick;
    }

    public SubmitOrderPayAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null,"");
    }

    public SubmitOrderPayAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, ConfirmOrderData data, String saleType) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.saleType = saleType;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View item = holder.itemView.findViewById(R.id.item);

        View rl_gold = holder.itemView.findViewById(R.id.rl_gold);
        View rl_gold_line_layout = holder.itemView.findViewById(R.id.rl_gold_line_layout);
        CheckBox checkBox_gold = holder.itemView.findViewById(R.id.checkBox_gold);
        TextView tv_gold_money = holder.itemView.findViewById(R.id.tv_gold_money);
        TextView tv_gold = holder.itemView.findViewById(R.id.tv_gold);
        ImageView iv_gold_mask = holder.itemView.findViewById(R.id.iv_gold_mask);
        ImageView iv_gold = holder.itemView.findViewById(R.id.iv_gold);

        ConfirmOrderPrice confirmOrderPrice = data.getPrice();
        ConfirmOrderCustomerAccount customerAccount = data.getCustomer_account();

        AddressMessage addressMessage = data.getAddress();
        String address_id = "0";
        if(addressMessage != null){
            if(!TextUtils.isEmpty(addressMessage.getAddress_id())){
                address_id = addressMessage.getAddress_id();
            }
        }

        if(data.isDisplay_gold()){
            rl_gold.setVisibility(View.VISIBLE);
            rl_gold_line_layout.setVisibility(View.VISIBLE);
            if (Float.valueOf(confirmOrderPrice.getDeduction_amount()) > 0) {
                String string = "可用" + confirmOrderPrice.getIntegral_deduction() + "金币抵扣" + confirmOrderPrice.getDeduction_amount() + "元 " ;
                if(Float.valueOf(customerAccount.getIntegral()) < 100){
                    string = "已扣除兑换商品所需金币，但商品待支付金额不足1元不可抵扣";
                    iv_gold_mask.setVisibility(View.VISIBLE);
                    checkBox_gold.setVisibility(View.GONE);
                }else {
                    iv_gold_mask.setVisibility(View.GONE);
                    checkBox_gold.setVisibility(View.VISIBLE);
                }
                //创建一个SpannableString对象，后面增加空格是预留给图片的
                SpannableString spannableString = new SpannableString(string + "  ");
                //创建图片的Drawable对象
                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.question_img);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                //创建图片的ImageSpan对象
                ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
                //设置ImageSpan，将其位置设置在spannableString最后面，起始位置start=spannableString.length() - 1，结束位置为end=spannableString.length(),
                // SPAN_INCLUSIVE_EXCLUSIVE表示包含start不包含end
                spannableString.setSpan(imageSpan, spannableString.length() - 1, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tv_gold_money.setText(spannableString);
                tv_gold.setTextColor(Color.parseColor("#141922"));
                tv_gold_money.setTextColor(Color.parseColor("#999FAA"));
                iv_gold.setAlpha(1f);

                checkBox_gold.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(onPayClick != null){
                            onPayClick.OnGoldChanged(isChecked);
                        }

                    }
                });
            } else {
                iv_gold_mask.setVisibility(View.VISIBLE);
                checkBox_gold.setVisibility(View.GONE);
                tv_gold.setTextColor(Color.parseColor("#C2C5CC"));
                tv_gold_money.setTextColor(Color.parseColor("#C2C5CC"));
                iv_gold.setAlpha(0.5f);
            }
        }else{
            rl_gold.setVisibility(View.GONE);
            rl_gold_line_layout.setVisibility(View.GONE);
        }

        ImageView iv_balance = holder.itemView.findViewById(R.id.iv_balance);
        TextView tv_balance = holder.itemView.findViewById(R.id.tv_balance);
        TextView tv_balance_tag = holder.itemView.findViewById(R.id.tv_balance_tag);
        TextView tv_balance_money = holder.itemView.findViewById(R.id.tv_balance_money);
        View iv_balance_mask = holder.itemView.findViewById(R.id.iv_balance_mask);
        CheckBox checkBox_balance = holder.itemView.findViewById(R.id.checkBox_balance);
        View rl_wallet = holder.itemView.findViewById(R.id.rl_wallet);
        View wallet_line = holder.itemView.findViewById(R.id.wallet_line);

        if (Float.valueOf(customerAccount.getBalance()) > 0) {
            tv_balance_money.setText("¥" + customerAccount.getBalance());
            iv_balance_mask.setVisibility(View.GONE);
            checkBox_balance.setVisibility(View.VISIBLE);
            tv_balance.setTextColor(Color.parseColor("#141922"));
            tv_balance_money.setTextColor(Color.parseColor("#999FAA"));
            tv_balance_tag.setTextColor(Color.parseColor("#999FAA"));
            iv_balance.setAlpha(1f);
            checkBox_balance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(onPayClick != null){
                        onPayClick.OnBalanceChanged(isChecked);
                    }
                }
            });
        } else {
            tv_balance_money.setText("¥0.00");
            iv_balance_mask.setVisibility(View.VISIBLE);
            checkBox_balance.setVisibility(View.GONE);
            tv_balance.setTextColor(Color.parseColor("#C2C5CC"));
            tv_balance_money.setTextColor(Color.parseColor("#C2C5CC"));
            tv_balance_tag.setTextColor(Color.parseColor("#C2C5CC"));
            iv_balance.setAlpha(0.5f);
        }
        if (Double.valueOf(confirmOrderPrice.getPay_price()) == 0 && Float.valueOf(confirmOrderPrice.getUse_balance()) == 0) {
            iv_balance_mask.setVisibility(View.VISIBLE);
            checkBox_balance.setVisibility(View.GONE);
            checkBox_balance.setChecked(false);
        }
        if(data.isDisplay_balance()){
            rl_wallet.setVisibility(View.VISIBLE);
            wallet_line.setVisibility(View.VISIBLE);
        }else{
            rl_wallet.setVisibility(View.GONE);
            wallet_line.setVisibility(View.GONE);
        }


        ImageView iv_red_pack = holder.itemView.findViewById(R.id.iv_red_pack);
        TextView tv_hb_tag = holder.itemView.findViewById(R.id.tv_hb_tag);
        TextView tv_hb = holder.itemView.findViewById(R.id.tv_hb);
        TextView tv_money = holder.itemView.findViewById(R.id.tv_money);
        View iv_hb_mask = holder.itemView.findViewById(R.id.iv_hb_mask);
        CheckBox checkBox_hb = holder.itemView.findViewById(R.id.checkBox_hb);

        View rl_gzb = holder.itemView.findViewById(R.id.rl_gzb);
        View line_pay_mode = holder.itemView.findViewById(R.id.line_pay_mode);
        View divider_line = holder.itemView.findViewById(R.id.divider_line);

        if (Float.valueOf(customerAccount.getBupiao()) > 0) {
            tv_money.setText("¥" + customerAccount.getBupiao());
            iv_hb_mask.setVisibility(View.GONE);
            checkBox_hb.setVisibility(View.VISIBLE);
            tv_hb.setTextColor(Color.parseColor("#141922"));
            tv_money.setTextColor(Color.parseColor("#999FAA"));
            tv_hb_tag.setTextColor(Color.parseColor("#999FAA"));
            iv_red_pack.setAlpha(1f);
            checkBox_hb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (Double.valueOf(confirmOrderPrice.getPay_price()) <= Double.valueOf(confirmOrderPrice.getUse_bupiao())) {
                            if (checkBox_balance.isChecked()) {
                                checkBox_balance.setChecked(false);
                            }
                        }
                    }
                    if(onPayClick != null){
                        onPayClick.OnGZBChanged(isChecked);
                    }
                }
            });
        } else {
            tv_hb_tag.setTextColor(Color.parseColor("#C2C5CC"));
            tv_hb.setTextColor(Color.parseColor("#C2C5CC"));
            tv_money.setTextColor(Color.parseColor("#C2C5CC"));
            tv_money.setText("¥0.00");
            iv_hb_mask.setVisibility(View.VISIBLE);
            checkBox_hb.setVisibility(View.GONE);
            iv_red_pack.setAlpha(0.5f);
        }

        if (Double.valueOf(confirmOrderPrice.getPay_price()) == 0 && Float.valueOf(confirmOrderPrice.getUse_bupiao()) == 0) {
            iv_hb_mask.setVisibility(View.VISIBLE);
            checkBox_hb.setVisibility(View.GONE);
            checkBox_hb.setChecked(false);
        }

        if(data.isDisplay_bupiao()){
            rl_gzb.setVisibility(View.VISIBLE);
            line_pay_mode.setVisibility(View.VISIBLE);
        }else{
            rl_gzb.setVisibility(View.GONE);
            line_pay_mode.setVisibility(View.GONE);
        }

        if(!data.isDisplay_bupiao() && ! data.isDisplay_balance() && !data.isDisplay_gold()){
            divider_line.setVisibility(View.GONE);
        }else{
            divider_line.setVisibility(View.VISIBLE);
        }

        if("0".equals(address_id) && "0".equals(data.getIs_hidden_address())){
            iv_gold_mask.setVisibility(View.VISIBLE);
            checkBox_gold.setVisibility(View.GONE);
            tv_gold.setTextColor(Color.parseColor("#C2C5CC"));
            tv_gold_money.setTextColor(Color.parseColor("#C2C5CC"));
            iv_gold.setAlpha(0.5f);

            iv_balance_mask.setVisibility(View.VISIBLE);
            checkBox_balance.setVisibility(View.GONE);
            tv_balance.setTextColor(Color.parseColor("#C2C5CC"));
            tv_balance_money.setTextColor(Color.parseColor("#C2C5CC"));
            tv_balance_tag.setTextColor(Color.parseColor("#C2C5CC"));
            iv_balance.setAlpha(0.5f);

            tv_hb_tag.setTextColor(Color.parseColor("#C2C5CC"));
            tv_hb.setTextColor(Color.parseColor("#C2C5CC"));
            tv_money.setTextColor(Color.parseColor("#C2C5CC"));
            iv_hb_mask.setVisibility(View.VISIBLE);
            checkBox_hb.setVisibility(View.GONE);
            iv_red_pack.setAlpha(0.5f);

        }

        //支付模块的展示
        View ll_pay = holder.itemView.findViewById(R.id.ll_pay);
        View rl_alipay = holder.itemView.findViewById(R.id.rl_alipay);
        View rl_wxpay = holder.itemView.findViewById(R.id.rl_wxpay);
        View view_pay_line = holder.itemView.findViewById(R.id.view_pay_line);
        CheckBox checkBox_zfb = holder.itemView.findViewById(R.id.checkBox_zfb);
        CheckBox checkBox_wx = holder.itemView.findViewById(R.id.checkBox_wx);
        View iv_ck_zfb_block = holder.itemView.findViewById(R.id.iv_ck_zfb_block);
        View iv_ck_wx_block = holder.itemView.findViewById(R.id.iv_ck_wx_block);
        List<PaymentModule> paymentModules = data.getPayments();
        if (paymentModules != null && paymentModules.size() > 0) {
            for (PaymentModule paymentModule : paymentModules) {
                if ("1".equals(paymentModule.getPayment_id())) {
                    rl_alipay.setVisibility(View.VISIBLE);
                }
                if ("2".equals(paymentModule.getPayment_id())) {
                    rl_wxpay.setVisibility(View.VISIBLE);
                }
            }
            if (paymentModules.size() == 2) {
                view_pay_line.setVisibility(View.VISIBLE);
            } else if (paymentModules.size() == 1) {
                view_pay_line.setVisibility(View.GONE);
            }

            checkBox_zfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String pay_way = "0";
                    if (isChecked) {
                        checkBox_wx.setChecked(false);
                        pay_way = "1";
                    }
                    if(onPayClick != null){
                        onPayClick.OnPayWay(pay_way);
                    }
                }
            });

            checkBox_wx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String pay_way = "0";
                    if (isChecked) {
                        checkBox_zfb.setChecked(false);
                        pay_way = "2";
                    }
                    if(onPayClick != null){
                        onPayClick.OnPayWay(pay_way);
                    }
                }
            });

            if(Float.valueOf(confirmOrderPrice.getPay_price()) <= 0){
                iv_ck_zfb_block.setVisibility(View.VISIBLE);
                iv_ck_wx_block.setVisibility(View.VISIBLE);
                checkBox_zfb.setVisibility(View.GONE);
                checkBox_wx.setVisibility(View.GONE);
            } else {
                iv_ck_zfb_block.setVisibility(View.GONE);
                iv_ck_wx_block.setVisibility(View.GONE);
                checkBox_zfb.setVisibility(View.VISIBLE);
                checkBox_wx.setVisibility(View.VISIBLE);
            }

        } else {
            ll_pay.setVisibility(View.GONE);
        }


        //几个说明
        ConfirmOrderInstructions instructions = data.getInstructions();
        View iv_balance_question = holder.itemView.findViewById(R.id.iv_balance_question);
        View iv_hb_question = holder.itemView.findViewById(R.id.iv_hb_question);
        iv_balance_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewActivityDialog dialog4 = new NewActivityDialog(mContext, "钱包账号说明", instructions.getBalance_introduce());
                dialog4.show();
            }
        });
        tv_gold_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewActivityDialog goldDialog = new NewActivityDialog(mContext, "金币说明", instructions.getGold_introduce());
                goldDialog.show();
            }
        });
        iv_hb_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewActivityDialog dialog4 = new NewActivityDialog(mContext, "向日葵币说明", instructions.getBupiao_introduce());
                dialog4.show();
            }
        });

        if (!TextUtils.isEmpty(saleType)&&("4".equals(saleType)||"6".equals(saleType))){
            checkBox_gold.setButtonDrawable(R.drawable.selector_dealer_duoxuan);
            checkBox_balance.setButtonDrawable(R.drawable.selector_dealer_duoxuan);
            checkBox_hb.setButtonDrawable(R.drawable.selector_dealer_duoxuan);
            checkBox_zfb.setButtonDrawable(R.drawable.selector_purchase_radio);
            checkBox_wx.setButtonDrawable(R.drawable.selector_purchase_radio);
        }

    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }
}
